//
//  InterfaceController.swift
//  WatchKitPreview WatchKit Extension
//
//  Created by 王儒林 on 15/8/27.
//  Copyright (c) 2015年 王儒林. All rights reserved.
//

import WatchKit
import Foundation

enum CalculationOperation {
    case plus,minus,multiply,divide,none
}

class InterfaceController: WKInterfaceController {
    
    static var sharedInstance : InterfaceController!
    
    
    @IBOutlet weak var laNumber: WKInterfaceLabel!
    var stringToDisplay = "0";
    var cachedNumber : Double = 0;
    var operationHasPressed : CalculationOperation = CalculationOperation.none;
    var justPressedOperation = false
    
    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)
        InterfaceController.sharedInstance = self
        // Configure interface objects here.
        if let key = NSUserDefaults.standardUserDefaults().stringForKey("key") {
            laNumber.setText(key)
        }
        
        addMenuItemWithImage(UIImage(named: "menu_item_0")!, title: "reset", action: "menuClick")
        addMenuItemWithItemIcon(WKMenuItemIcon.Share, title: "nav跳转", action: "naviToList")
        addMenuItemWithItemIcon(WKMenuItemIcon.Mute, title: "mod跳转", action: "modalToList")
        
    }
    func naviToList(){
        pushControllerWithName("ListInterfaceController", context: nil)
    }
    func modalToList(){
        presentControllerWithName("ListInterfaceController", context: nil)
    }
    func menuClick(){
        clearClick()
    }
    override func willActivate () {
        super.willActivate()
//        var name : CFString = "com.wrl.testwatch"
//        CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, notified, name, NULL, CFNotificationSuspensionBehavior.DeliverImmediately)
    }
    override func didDeactivate () {
        super.didDeactivate()
        var name : CFString
//        CFNotificationCenterRemoveObserver(center:CFNotificationCenterGetDarwinNotifyCenter(), observer:NULL, name:"com.wrl.testwatch", object:NULL)
    }
    @IBAction func clearClick() {
        stringToDisplay = "0"
        updateDisplay()
    }

    @IBAction func num0Click() {
        appendNum("0")
    }
    @IBAction func num1Click() {
        appendNum("1")
    }
    @IBAction func num2Click() {
        appendNum("2")
    }
    @IBAction func num3Click() {
        appendNum("3")
    }
    @IBAction func num4Click() {
        appendNum("4")
    }
    @IBAction func num5Click() {
        appendNum("5")
    }
    @IBAction func num6Click() {
        appendNum("6")
    }
    @IBAction func num7Click() {
        appendNum("7")
    }
    @IBAction func num8Click() {
        appendNum("8")
    }
    @IBAction func num9Click() {
        appendNum("9")
    }
    @IBAction func numDotClick() {
        appendNum(".")
    }
    
    @IBAction func methodPlusClick() {
        cacheNumber()
        operationHasPressed = CalculationOperation.plus
        justPressedOperation = true
    }
    @IBAction func methodMinusClick() {
        cacheNumber()
        operationHasPressed = CalculationOperation.minus
        justPressedOperation = true
    }
    @IBAction func methodMultiplyClick() {
        cacheNumber()
        operationHasPressed = CalculationOperation.multiply
        justPressedOperation = true
    }
    @IBAction func methodDivideClick() {
        cacheNumber()
        operationHasPressed = CalculationOperation.divide
        justPressedOperation = true
    }
    
    func cacheNumber() {
        cachedNumber = (stringToDisplay as NSString).doubleValue;
    }
    
    func appendNum(num : String) ->Bool{
        
        if !justPressedOperation {
            if stringToDisplay == "0" {
                stringToDisplay = num
            } else if num == "." && stringToDisplay.rangeOfString(".") != nil {
                stringToDisplay += "."
            } else {
                stringToDisplay += num
            }
        } else {
            if num == "." {
                stringToDisplay = "0."
            } else {
                stringToDisplay = num
            }
            justPressedOperation = false
        }
        updateDisplay()
        return false;
    }
    @IBAction func caculateClick() {
        var preNum : Double = cachedNumber
        var nextNum : Double = (stringToDisplay as NSString).doubleValue
        var result : Double = 0;
        switch operationHasPressed {
        case CalculationOperation.plus :
            result = preNum + nextNum
        case CalculationOperation.minus :
            result = preNum - nextNum
        case CalculationOperation.minus :
            result = preNum * nextNum
        case CalculationOperation.minus :
            result = preNum / nextNum
        default :
            result = nextNum
        }
        stringToDisplay = result.description
        updateDisplay()
        operationHasPressed = CalculationOperation.none
    }
    
    func updateDisplay() {
        laNumber.setText(stringToDisplay)
    }
}
